package com.topfinance.payment.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.topfinance.frame.dao.HibernateEntityDao;
import com.topfinance.frame.exception.AppException;
import com.topfinance.frame.util.StringUtil;
import com.topfinance.payment.ebo.TC2MsgInoutEbo;
import com.topfinance.payment.service.ITC2MsgInoutMgr;

/**
 * 该类自动实现了dao的一般方法，如add, remove, update
 * 在service中出现的大部分情况下是在向后台提交数据前的处理(如校验)及获取后台数据后的处理等
 * @author
 *
 */

public class TC2MsgInoutMgrImpl extends HibernateEntityDao<TC2MsgInoutEbo> implements ITC2MsgInoutMgr {

	private static final Log log = LogFactory.getLog(TC2MsgInoutMgrImpl.class);

	//获得二代业务报文匹配关系
	public List<TC2MsgInoutEbo> getMsgInoutPair(String msg, String model) throws AppException{
		String hql = " from TC2MsgInoutEbo m " ;
		String where = "";
		if (msg != null)
			where += " and m.outMsgCode ='" + msg + "'";
		if (model != null) 
			where += " and m.poMode = '" + model + "'";
		List<Object> paramList = new ArrayList<Object>();
		if (isNotBlank(where)) {
			where = " where " + where.substring(4);
		}
		hql = hql + where;
		return find(hql,paramList.toArray());		
	}
	
	//S=发起|模式一，R=接收|模式二
	public String getMsgMode(String tpcode, String apptype) throws AppException{
		String hql = " select m.msgCode from TC2BisTpMsgEbo m " ;
		String where = "";
		if (StringUtil.isNotNullStr(tpcode))
			where += " and m.tpCode ='" + tpcode + "'";
		if (StringUtil.isNotNullStr(apptype))
			where += " and m.appType = '" + apptype + "'";
		where += " and m.isAcitve = 'Y'";
		List<Object> paramList = new ArrayList<Object>();
		if (isNotBlank(where)) {
			where = " where " + where.substring(4);
		}
		hql = hql + where;
		List<String> msgs = find(hql,paramList.toArray());
		String model = "";
		for(String msg :msgs){
			hql = "select m.po from TC2MsgInoutEbo m";
			where = " and m.outMsgCode ='" + msg + "'";
			if (StringUtil.isNotNullStr(apptype))
				where += " and m.appType = '" + apptype + "'";
			if (isNotBlank(where)) {
				where = " where " + where.substring(4);
			}
			List<Integer> pos = find(hql+where,paramList.toArray());
			for(Integer po:pos){
				if(po.intValue() == TC2MsgInoutEbo.MODEL_1)
					model += "S,";
				else if(po.intValue() == TC2MsgInoutEbo.MODEL_2)
					model += "R,";
				else if(po.intValue() == TC2MsgInoutEbo.MODEL_3)
					model += "SR,";
			}
			//System.out.println(msg);
		}
		if(model.length() == 0)
			return "N";
		else
			return model.substring(0,model.length()-1);
	}

	//根据业务类型和应用模块获得对应的报文
	public String getMsgCode(String tpcode, String apptype) throws AppException{
		String hql = " select m.msgCode from TC2BisTpMsgEbo m " ;
		String where = "";
		if (StringUtil.isNotNullStr(tpcode))
			where += " and m.tpCode ='" + tpcode + "'";
		if (StringUtil.isNotNullStr(apptype))
			where += " and m.appType = '" + apptype + "'";
		//where += " and m.isAcitve = 'Y'";
		List<Object> paramList = new ArrayList<Object>();
		if (isNotBlank(where)) {
			where = " where " + where.substring(4);
		}
		hql = hql + where;
		List<String> msgs = find(hql,paramList.toArray());
		if(msgs.size()>1){
			//throw new AppException("[业务种类报文对应数据错]" + tpcode+","+apptype);
			System.out.println("[业务种类报文对应数据错]" + tpcode+","+apptype);
			//暂时默认处理吧！！！
			return msgs.get(0);
		}else if(msgs.size() == 1)
			return msgs.get(0);
		else
			return null;
	}
	
}
